beta<-read.table("merged_beta.txt",sep="\t",head=T,colClasses=c("character",rep("numeric",2661)),stringsAsFactors =F,na.strings=c('-'))
sitenames<-c("BREAST","COLON","GLIOBLASTOMA","KIDNEY RENAL CLEAR","KIDNEY RENAL PAPILLARY","BLOOD LEUKEMIA","LUNG ADENOMA","LUNG SQUAMOUS","OVARY","RECTUM","STOMACH","UTERUS")
pdf("withinbatch.pdf",width=15, height=8)
i<-1
while(i<=length(table(a1[,1])))
{
	print(i)
	print(names(table(a1[,1]))[i])
	temp<-c()
	temp<-a1[which(a1[,1]==names(table(a1[,1]))[i]),]
	k<-1
	data<-c()
	col<-c()
	m<-length(table(temp[,7]))
	if(m>6)
	{
		m<-6
	}
	while(k<=m)
	{
		min<-20
		if(min > length(which(temp[,7]==as.character(names(table(temp[,7]))[k]))))
		{
			min<-length(which(temp[,7]==as.character(names(table(temp[,7]))[k])))
		}
		#print(min)
		temp1<-temp[which(temp[,7]==as.character(names(table(temp[,7]))[k]))[1:min],]
		data<-rbind(data,temp1)
		col<-c(col,rep(k+1,min))
		k<-k+1
	}
	j<-1
	temp1<-c()
	while(j<=nrow(data))
	{
		temp1[j]<-paste(data[j,],collapse='.')
		j<-j+1
	}
	temp<-c()
	temp<-beta[,temp1]
	rownames(temp)<-beta[,1]
	main<-paste('Tissue',names(table(a1[,1]))[i],sitenames[i],sep='-')
	if(length(table(col))>7)
	{
		col[which(col==9)]<-4
	}
	print(col)
	boxplot(temp,horizontal=FALSE,show.names = FALSE,main=main,xlab="SAMPLES",ylab="Beta",ylim=c(0,1),col=col)
	i<-i+1
}
dev.off()	
	
#	temp<-temp[which(temp[,7]==as.character(names(sort(table(temp[,7]),decreasing = TRUE))[1])),]#
#	batch<-names(sort(table(temp[,7])))[1]
#	if(nrow(temp)>100)
#	{
#		temp<-temp[1:100,]
#	}
	
	